package com.dove.authority.mapper;

import com.dove.authority.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dove.authority.entity.bo.RoleBo;
import com.dove.authority.entity.vo.RoleVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author run
 * @since 2021-03-18
 */
public interface RoleMapper extends BaseMapper<Role> {

    @Select("SELECT role_id FROM t_authority_role_user taru INNER JOIN t_authority_role tar ON " +
            "taru.userId = #{userId} AND tar.id = taru.role_id AND tar.usable = 1")
    public List<Long> findRoleIdsOfUser(Long userId);

    @Select("SELECT COUNT(*) FROM t_authority_role WHERE name = #{name} AND enterprise_id = #{enterpriseId}")
    public int getCountByNameAndEnterprise(String name, Long enterpriseId);

    @Delete("DELETE FROM t_authority_role WHERE id = #{roleId} AND enterprise_id = #{enterpriseId}")
    public int deleteRoleByIdAndEnterprise(Long roleId, Long enterpriseId);

    @Select("SELECT id, name, remarks, usable FROM t_authority_role WHERE enterprise_id = #{enterpriseId}")
    public List<RoleVo> findRoleVoOfEnterprise(Long enterpriseId);

    @Update("UPDATE t_authority_role SET name = #{role.name}, remarks = #{role.remarks} WHERE id = #{role.id} " +
            "AND enterprise_id = #{enterpriseId}")
    public int updateRole(RoleBo role, Long enterpriseId);


}
